function AppController(){
	this.rooms=[];
	this.user=null;
	this.currentRoom=null;
	this.users=[];
}

AppController.prototype.init = function(){
	this.rooms.push(new Room());
	this.user=new User();
	this.currentRoom=this.rooms[0];
};

function listenerAdd(overlay){
	$("#blogId").val(overlay);
	$('#title').val('');
	$('#description').val('');
	$("#publishTime").html('');
	$("#score").html('');
	$("#media").html('');
	$("#media").addClass("media");
	$("#media").removeClass("row");
	$("#media").removeClass("mediaStatic");
	$("#addMedia").click();
	$("#mediaButton").show();
	$("#socialButton").hide();
	$("#actionButton").show();
	$("#listComments").hide();
	$("#addCommentArea").hide();
	$("#div-comment").css({
		height:'33%',
	});
	$("#tags").show();
	$("#restaurant").prop("checked",false);
	$("#stress").prop("checked",false);
	$("#commuting").prop("checked",false);
	$("#nightlife").prop("checked",false);
	$("#accident").prop("checked",false);
	
	if($("#panelD").css("display")=='none')
		$("#buttonD").click();
}
function preListenerAdd(){
	$('#title').val('');
	$('#description').val('');
	if($("#panelD").css("display")=='block')
		  $("#buttonD").click();
}

function loadBlog(blogId){
	for(var j=0;j<app.currentRoom.publicBlogs.length;j++){
		if(app.currentRoom.publicBlogs[j].id==blogId){
			return app.currentRoom.publicBlogs[j];			
		}				
	}
}

function loadImage(files){
	if(files[0]!=null){
		for(var i=0;i<files.length;i++){
			$("#media").html($("#media").html()+
			"<div class='col-xs-6 col-md-4'>"+
			  "<a class='fancybox' rel='group' href='"+files[i]+"' style='width: 50px; height: 50px;'>"+
			    "<img class='media-object img-thumbnail' style='width: 50px; height: 50px;' src='"+files[i]+"'></a>"+
			"</div>");
		}
		$("#media").addClass("mediaStatic");
		$("#div-comment").css({
			height:'32%',
		});
	}else{
		$("#div-comment").css({
			height:'47%',
		});
	}
}

function loadComment(blog){
	for(var k=blog.comments.length-1;k>=0;k--){
		$('#comments').html($('#comments').html()+
				"<div class='cuerpo'>"+
					/*"<div class='imagen'>"+
					"</div>"+ */
					"<div class='texto'>"+blog.comments[k].body+
						"<div class='hora'>"+blog.comments[k].userId+" at "+blog.comments[k].time+"</div>"+
					"</div>"+
				"</div>");
	}	
}
function listenerPublish(blogId,title,description,files,time){
	var blog=loadBlog(blogId);
	$("#blogId").val(blogId);
	$("#title").val(title);
	$("#description").val(description);
	$("#publishTime").html("Publish at "+time);
	$("#score").html("Score: "+(blog.likes-blog.dislikes));
	$("#media").html('');
	$("#media").removeClass("media");
	$("#media").removeClass("mediaStatic");
	$("#media").addClass("row");
	loadImage(files);
	$('#comments').html('');
	loadComment(blog);
	$("#mediaButton").hide();
	$("#tags").hide();
	$("#socialButton").show();
	$("#likes").val(blog.likes);
	$("#dislikes").val(blog.dislikes);
	$("#actionButton").hide();
	$("#listComments").show();
	$("#addCommentArea").show();
	if($("#panelD").css("display")=='none')
		$("#buttonD").click();
}
function resetField(){
	$('#title').val('');
	$('#description').val('');
	$("#buttonD").click();
	$("#media").html('');
	$("#addMedia").click();
}
function addLikeByBlogId(blogId){
	var blog=loadBlog(blogId);
	blog.likes++;
	if($("#panelD").css("display")=='block' && $("#blogId").val()==blogId){
		$("#likes").val(blog.likes);
		$("#score").html("Score: "+(blog.likes-blog.dislikes));
	}
}
function addDislikeByBlogId(blogId){
	var blog=loadBlog(blogId);
	blog.dislikes++;
	if($("#panelD").css("display")=='block' && $("#blogId").val()==blogId){
		$("#dislikes").val(blog.dislikes);
		$("#score").html("Score: "+(blog.likes-blog.dislikes));
	}
}

function addOneComment(blogId,userName,body,time){
	if($("#panelD").css("display")=='block' && $("#blogId").val()==blogId){
		$('#comments').html("<div class='cuerpo'>"+
					/*"<div class='imagen'>"+
					"</div>"+ */
					"<div class='texto'>"+body+
						"<div class='hora'>"+userName+" at "+time+"</div>"+
					"</div>"+
				"</div>"+
				$('#comments').html());
	}
}
function setOtherPosition(userName,lat,lng){
	var pos = new google.maps.LatLng(lat,lng);
	var marker=new google.maps.Marker({
        clickable: true,
        position: pos,
        icon: 'resources/images/group-2.png',
        shadow: null,
        zIndex: 999,
        map: app.currentRoom.map 
    });
	var infowindow = new google.maps.InfoWindow({
	    content: userName
	});
	google.maps.event.addListener(marker, 'click', function() {
		  infowindow.open(app.currentRoom.map,marker);
	});
	var n=app.users.length;
	var user=new User();
	user.myLocation=marker;
	user.name=userName;
	app.users[n]=user;	
}
function changeOtherPosition(user,lat,lng){
	var pos = new google.maps.LatLng(lat,lng);
	user.myLocation.setPosition(pos);
}
function getTime(){
	var date=new Date();
	return date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()+
	" "+date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear();	
}
function createMatchingList(matchingBlogs){
	$("#matchingList").html("<a href='#' class='close'>[X]</a><span class='input-group-addon'>Results</span>");
	for(var i=0;i<matchingBlogs.length;i++){		
		$("#matchingList").html($("#matchingList").html()+"<button id='buttonList' type='button' class='btn btn-default buttonList' value="+matchingBlogs[i].id+">"+matchingBlogs[i].id+" "+matchingBlogs[i].title+"</button>");
	}
}
function createMyMatchingList(matchingBlogs){
	$("#matchingList").html("<a href='#' class='close'>[X]</a><span class='input-group-addon'>My Blogs</span>");
	for(var i=0;i<matchingBlogs.length;i++){		
		$("#matchingList").html($("#matchingList").html()+"<button id='buttonList' type='button' class='btn btn-default buttonList' value="+matchingBlogs[i].id+">"+matchingBlogs[i].id+" "+matchingBlogs[i].title+"</button>");
	}
}